import org.jsoup.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by Administrator on 2014/3/25.
 * Time: 15:51
 */
public class demo1_jsoup {
    public static void main(String[] args) {
        String  url="http://oa.raycloud.com/login.php";

        Connection connection  = Jsoup.connect(url);
        connection.timeout(1000*20);
        Connection.Response response= null;
        Map<String, String> cookiemap=null;
        Map<String,String>  datamap=null;
        try {
            response = connection.execute();
            Map<String, String> map = response.cookies();
            String key;
            Iterator<String> keys = map.keySet().iterator();

            System.out.println("-----------------------coookie-------------------------");
            cookiemap = new HashMap<String, String>();
            while(keys.hasNext())
            {
                key = keys.next();
                cookiemap.put(key, map.get(key));
                System.out.println(key+"="+map.get(key));
            }
            Document document=connection.get();
            datamap=new HashMap<String, String>();
            Elements els = document.select("form#loginform  > input");
            for (Element el : els) {
                System.out.println(el.attr("name")+"   ==>"+el.attr("value"));
                datamap.put(el.attr("name"),el.attr("value"));
            }

            datamap.put("user_name", "左韵" );
            datamap.put("user_passwd","jik920717");



//            System.out.println(document);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (response != null) {
            //                System.out.println(response.parse());
            System.out.println("============charset===============");
            System.out.println(response.charset());
            System.out.println("=============statusCode==============");
            System.out.println(response.statusCode());
            System.out.println("=============url==============");
            System.out.println(response.url());
            System.out.println("=============cookies==============");
            System.out.println(response.cookies());
            System.out.println("=============headers==============");
            System.out.println(response.headers());
            System.out.println("===========================");


        }



        Connection connection2  = Jsoup.connect("http://oa.raycloud.com/login.php");
        connection2.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        connection2.header("Accept-Encoding","gzip, deflate");
        connection2.header("Accept-Language","zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
        connection2.header("Connection","keep-alive");
        connection2.header("Referer","http://oa.raycloud.com/login.php");
        connection2.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0");
        connection2.cookies(cookiemap);
        connection2.data(datamap);
        try {
            connection2.post();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Connection.Response response2 = connection2.response();

        try {
            System.out.println(response2.parse());
        } catch (IOException e) {
            e.printStackTrace();
        }
            System.out.println("============charset===============");
            System.out.println(response.charset());
            System.out.println("=============statusCode==============");
            System.out.println(response.statusCode());
            System.out.println("=============url==============");
            System.out.println(response.url());
            System.out.println("=============cookies==============");
            System.out.println(response.cookies());
            System.out.println("=============headers==============");
            System.out.println(response.headers());
            System.out.println("===========================");



    }
}
